import logging
from commens import get_back_data, reMML, dump_to_json

ALMdata = None
MMLdata = None


def find_mml(ALMobj):
    if "处理步骤" in ALMobj.keys():
        res = []
        for line in ALMobj['处理步骤'].split('。'):
            res.append((reMML.findall(line),line))
        return res

def main():
    global ALMdata
    global MMLdata
    res = {}
    ALMdata = get_back_data('ALM_MAPPING.json')
    MMLdata = get_back_data('../PARAM.json')
    for ALMk,ALMv in ALMdata.items():
        logMAPPING.debug('●%s' % ALMk)
        res[ALMk] = {
            '参数':ALMv['告警参数'] if '告警参数' in ALMv.keys() else None ,
            '尾命令': {}
        }
        mmlTuples = find_mml(ALMv)
        for t in mmlTuples:
            for x in t[0]:
                logMAPPING.debug('[%s] %s'%(x,t[1]))
                if x in MMLdata.keys():
                    if x not in res[ALMk]['尾命令'].keys():

                        res[ALMk]['尾命令'][x] = {}
                        res[ALMk]['尾命令'][x]["参数"] = MMLdata[x]
                        res[ALMk]['尾命令'][x]['关联内容'] = t[1]
                    else:
                        res[ALMk]['尾命令'][x]['关联内容'] += ('。'+t[1])
        logMAPPING.debug('共有%s条尾命令'%len(res[ALMk]['尾命令']))
    dump_to_json(res,'ALM_EXTRACT_1.json')
    print("生成了%d条数据"%len(res))





if __name__ == '__main__':
    logMAPPING = logging.getLogger("ALM_EXTRACT")
    logMAPPING.setLevel(logging.DEBUG)
    hdMAPPING = logging.FileHandler("ALM_EXTRACT.log", mode='w', encoding='utf-8')
    hdMAPPING.setLevel(logging.DEBUG)
    logMAPPING.addHandler(hdMAPPING)
    main()